package com.jrx.anytxn.investor.timer.job.queryCreditResult.step;

import com.jrx.anytxn.common.uitl.InternetUtils;
import com.jrx.anytxn.common.uitl.JsonUtils;
import com.jrx.anytxn.common.uitl.SeqUtils;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.feign.gateway.bean.ApiResponse;
import com.jrx.anytxn.feign.gateway.request.PartnerRequest03;
import com.jrx.anytxn.feign.gateway.response.PartnerResponse03;
import com.jrx.anytxn.feign.gateway.service.IInnerGatewayService;
import com.jrx.anytxn.feign.param.request.HandleInvestorLimitReq;
import com.jrx.anytxn.feign.param.service.IAnyTxnParamService;
import com.jrx.anytxn.investor.bean.InvestorQueryCreditResultBean;
import com.jrx.anytxn.investor.constant.TransBizConstant;
import com.jrx.anytxn.investor.entity.TlCustomerInvestorLimit;
import com.jrx.anytxn.investor.entity.TlInvestorLimitApproveLog;
import com.jrx.anytxn.investor.service.IInvestorCustomerLimitService;
import com.jrx.anytxn.param.constant.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 查询合作行授信结果 - processor
 *
 * @author liuyang
 * @date 2020-06-09
 */
@Service
public class InvestorQueryCreditResultProcessor implements ItemProcessor<TlInvestorLimitApproveLog, InvestorQueryCreditResultBean> {

    Logger logger = LoggerFactory.getLogger(InvestorQueryCreditResultProcessor.class);

    @Lazy
    @Autowired
    private IInnerGatewayService innerGatewayService;

    @Lazy
    @Autowired
    private IAnyTxnParamService anyTxnParamService;

    @Autowired
    private IInvestorCustomerLimitService investorCustomerLimitService;

    /**
     * 查询合作行授信结果处理
     *
     * @param investorLimitApproveLog
     * @return TlInvestorLimitApproveLog
     * @throws Exception
     * @author liuyang
     * @date 2020-06-09
     */
    @Override
    public InvestorQueryCreditResultBean process(TlInvestorLimitApproveLog investorLimitApproveLog) throws Exception {

        // 初始化参数
        InvestorQueryCreditResultBean bean = new InvestorQueryCreditResultBean();
        String investorId = investorLimitApproveLog.getInvestorId();
        String tenantId = investorLimitApproveLog.getTenantId();

        // 初始化授信结果查询请求参数
        PartnerRequest03 req03 = new PartnerRequest03();
        req03.setServiceId(TransBizConstant.GATEWAY_SERVICE_ID); // 请求系统Id
        req03.setServiceIp(InternetUtils.getLocalIp()); // 请求ip地址
        req03.setTenantId(tenantId); // 租户Id
        req03.setServiceSeq(SeqUtils.getSN()); // 请求流水号
        req03.setInvestorId(investorId); // 出资方ID
        req03.setCreditSeq(investorLimitApproveLog.getSeqNo());// 授信流水号

        // 调用出资方授信结果查询接口
        ApiResponse<PartnerResponse03> resp = innerGatewayService.queryPartnerCreditResult(JsonUtils.toJSon(req03));

        // 获取出资方授信审批状态
        String approveStatus = investorLimitApproveLog.getApproveStatus();
        if (resp != null && resp.getData() != null && StringUtils.isNotEmpty(resp.getData().getApproveStatus())) {
            approveStatus = resp.getData().getApproveStatus();
        }


        // 如果授信结果成功，则保存客户资金源授信额度
        if (TransBizConstant.APPROVE_STATUS_S.equals(approveStatus)) {
            TlCustomerInvestorLimit customerInvestorLimit = investorCustomerLimitService.buildCustomerInvestorLimit(investorLimitApproveLog);
            bean.setCustomerInvestorLimit(customerInvestorLimit);
        }

        // 如果为拒绝，则恢复出资方授信额度
        if (TransBizConstant.APPROVE_STATUS_F.equals(approveStatus)) {
            HandleInvestorLimitReq req = new HandleInvestorLimitReq(tenantId, investorLimitApproveLog.getSeqNo(), investorLimitApproveLog.getChannel(),
                    investorId, investorLimitApproveLog.getProductId(), investorLimitApproveLog.getCreditLimit(), Constant.INVESTOR_LIMIT_CREDITTYPE_C, Constant.INVESTOR_LIMIT_USETYPE_R);
            anyTxnParamService.handleInvestorLimit(req);
        }

        // 如果授信结果已获得，则更新客户出资方授信审批记录
        if (!investorLimitApproveLog.getApproveStatus().equals(approveStatus)) {
            TlInvestorLimitApproveLog updateLog = new TlInvestorLimitApproveLog();
            updateLog.setId(investorLimitApproveLog.getId());
            updateLog.setTenantId(tenantId);
            updateLog.setOrganizationId(investorLimitApproveLog.getOrganizationId());
            updateLog.setCustomerId(investorLimitApproveLog.getCustomerId());
            updateLog.setApproveStatus(approveStatus);
            bean.setUpdateInvestorLimitApproveLog(updateLog);
        }

        return bean;
    }
}
